 package com.example.haiti.infrastructure.surveyor;

import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;

public class httpPostHelper extends Activity{
		
	public static final String DATABASE_WEBSITE_ADDRESS = "http://www.nd.edu/~ahuus/HIS/insertDatabase.php";          // 1
	//public static final String DATABASE_WEBSITE_ADDRESS = "http://epicsprojects.org/haiti_test/haiti_post.php";          // 1
	
	private Context mContext;
	private Cursor structureCursor = null;
	private Utils utilsHelper = null;
	
	// Variables that are to be posted
	
	private String structureName_value = null;
	private String structureDesc_value = null;
	private String structureAddress_value = null;
	private String surveyorName_value = null;
	private String phoneNumber_value = null;
		
    private String structureOwned_value = null;
    private String yearBuilt_value = null;
    private String buildDuration_value = null;
    private String builder_value = null;
    
    private String steelWalls_value = null;
    private String nFloors_value = null;
    private String roofType_value = null;
    private String nChildren_value = null;
    private String nAdults_value = null;
    
    private String photoFront_value = null;
    private String noteFront_value = null;
    
    private String photoRight_value = null;
    private String noteRight_value = null;    
    private String photoBack_value = null;
    private String noteBack_value = null;
    
    private String photoLeft_value = null;
    private String noteLeft_value = null;
    		
    public httpPostHelper(Context context) {
        mContext = context;
    }
    
    public void postToRemoteDatabase(int structureID){
    	
    	loadStructureData(structureID);
    	
    	new  httpPostData().execute();    	    	   	
    }
 
    private Cursor getStructureCursor(int structureID){
    	
    	Cursor mCursor;
    	
    	utilsHelper = new Utils(mContext);
        utilsHelper.openDatabase(); 	
    	mCursor = utilsHelper.getStructureCursor(structureID);
    	
    	return mCursor;
    }

    private void loadStructureData(int structureID){
    	
    	// Gets the cursor for the structure that is to be sent
    	structureCursor = getStructureCursor(structureID);
    	    
    	structureName_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NAME));
    	structureDesc_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_DESC));
    	structureAddress_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_ADDRESS));
    	surveyorName_value =  structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_SURVEYOR_NAME));
    	phoneNumber_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_SURVEYOR_PHONE_NUMBER));
    		
        structureOwned_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_OWNED));
        yearBuilt_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_YEAR_BUILT));
        buildDuration_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BUILD_DURATION));
        builder_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_BUILDER));
        
        steelWalls_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_STEEL_WALLS));
        nFloors_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_FLOORS));
        roofType_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_ROOF_TYPE));
        nChildren_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_CHILDREN));
        nAdults_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_ADULTS));
        
        photoFront_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_PHOTO_FRONT));
        noteFront_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NOTE_FRONT));
        
        photoRight_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_PHOTO_RIGHT));
        noteRight_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NOTE_RIGHT));
        
        photoBack_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_PHOTO_BACK));
        noteBack_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NOTE_BACK));
        
        photoLeft_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_PHOTO_LEFT));
        noteLeft_value = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NOTE_LEFT));
    	   
    } 
    
 private class httpPostData extends AsyncTask<Void, Void, String> {
        
    	private ProgressDialog pdia;
        
    	@Override
        protected void onPreExecute(){ 
                pdia = new ProgressDialog(mContext);
                pdia.setMessage("Uploading Structure Data ...");
                pdia.show();    
        }

        protected String doInBackground(Void... params) {
            	
            // This is executed in the background. POST is happening
            try { 
        	// MPE = multi part entity
           	 
         	     HttpClient httpClient = new DefaultHttpClient();
         	     HttpPost postRequest = new HttpPost(DATABASE_WEBSITE_ADDRESS);
         	     MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
         	     
         	 // Creates the quesetionnaire StringBody objects
         	     
         	     StringBody structureName = new StringBody(structureName_value);       	    
         	     StringBody structureDesc = new StringBody(structureDesc_value);
         	     StringBody structureAddress = new StringBody(structureAddress_value);
         	     StringBody surveyorName = new StringBody(surveyorName_value);
         	     StringBody phoneNumber = new StringBody(phoneNumber_value);

         	     StringBody structureOwned = new StringBody(structureOwned_value);
         	     StringBody yearBuilt = new StringBody(yearBuilt_value);
         	     StringBody buildDuration = new StringBody(buildDuration_value);
         	     StringBody builder = new StringBody(builder_value);

         	     StringBody steelWalls = new StringBody(steelWalls_value);
         	     StringBody nFloors = new StringBody(nFloors_value);
         	     StringBody roofType = new StringBody(roofType_value);
         	     StringBody nChildren = new StringBody(nChildren_value);
         	     StringBody nAdults = new StringBody(nAdults_value);
/*     	     
         	     StringBody noteFront = new StringBody(noteFront_value);
         	     StringBody noteRight = new StringBody(noteRight_value);
         	     StringBody noteBack = new StringBody(noteBack_value);
         	     StringBody noteLeft = new StringBody(noteLeft_value);
*/         	     
         	// Creates the photo FileBody objects
         	     FileBody photoFront = new FileBody(new File(photoFront_value));
         	     FileBody photoRight = new FileBody(new File(photoRight_value));
         	     FileBody photoBack = new FileBody(new File(photoBack_value));
         	     FileBody photoLeft = new FileBody(new File(photoLeft_value));

         	//***************** ADD to MPE ********************************************     
         	     
         	     // Add string data to MPE
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_NAME, structureName);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_DESC, structureDesc);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_ADDRESS, structureAddress);
         	     multipartContent.addPart(SqlHelper.COLUMN_SURVEYOR_NAME, surveyorName);
         	     multipartContent.addPart(SqlHelper.COLUMN_SURVEYOR_PHONE_NUMBER, phoneNumber);

         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_OWNED, structureOwned);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_YEAR_BUILT, yearBuilt);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_BUILD_DURATION, buildDuration);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_BUILDER, builder);

         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_STEEL_WALLS, steelWalls);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_FLOORS, nFloors);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_ROOF_TYPE, roofType);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_CHILDREN, nChildren);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_ADULTS, nAdults);
/*
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_NOTE_FRONT, noteFront);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_NOTE_RIGHT, noteRight);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_NOTE_BACK, noteBack);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_NOTE_LEFT, noteLeft);
 */     	     
         	     // Add the photos to the MPE
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_PHOTO_FRONT, photoFront);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_PHOTO_RIGHT, photoRight);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_PHOTO_BACK, photoBack);
         	     multipartContent.addPart(SqlHelper.COLUMN_STRUCTURE_PHOTO_LEFT, photoLeft);

         	     // Execute the POST
         	     postRequest.setEntity(multipartContent);
         	     HttpResponse res = httpClient.execute(postRequest);
         	     res.getEntity().getContent().close();
         	     
         	            	                   	            	
           	} catch (Throwable e) {
        	     // handle exception here
        	  e.printStackTrace();
        	 }
           	
            return null;
        }

        protected void onPostExecute(final String result) {
// REQ 3.5.4.R1 met.  The  dialog progress bar is not removed until a verification from the server has been received postExectue
        	pdia.dismiss(); 

        }
    }	// End of ASync Task
  
} // EOF